import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_search/flutter_baidu_mapapi_search.dart';
import 'package:get/get.dart';

class RoundedSearchField extends GetView<RoundedSearchFieldController> {
  RoundedSearchField({Key? key, required this.onSearchResult})
      : super(key: key);
  void Function(BMFSuggestionInfo) onSearchResult;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: RoundedSearchFieldController(onSearchResult: onSearchResult),
        builder: (RoundedSearchFieldController ctl) => Obx(() {
              return SizedBox(
                height: ctl.height.value,
                child: Column(
                  children: [
                    BrnSearchText(
                      focusNode: ctl.focusNode,
                      controller: ctl.textController,
                      searchController: ctl.searchController,
                      onTextClear: () {
                        ctl.focusNode.unfocus();

                        return false;
                      },
                      // autoFocus: true,
                      onActionTap: () {
                        //点击取消之后
                        ctl.recoveryState();
                      },
                      onTextCommit: (text) {
                        //提交
                        ctl.height.value = double.infinity;
                        ctl.searchLocation(text);
                        ctl.searchController.isActionShow = true;
                      },
                      onTextChange: (text) {
                        // BrnToast.show('输入内容 : $text', context);
                      },
                    ),
                    Container(
                      height: ctl.count.value * 70.0,
                      child: ListView.builder(
                        itemCount: ctl.count.value,
                        itemBuilder: (context, index) {
                          return InkWell(
                            onTap: () {
                              BrnToast.show(
                                  '点击了${ctl.suggestList[index].key}  ${ctl.suggestList[index]}',
                                  context);
                              //回调函数，将搜索结果返回给父组件
                              ctl.onSearchResult(
                                  ctl.suggestList[index]!);
                              ctl.recoveryState();
                            },
                            child: ListTile(
                              title: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(ctl.suggestList[index].key!),
                                  Text(
                                    ctl.suggestList[index].district!,
                                    style: TextStyle(color: Colors.black12),
                                  )
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    )
                  ],
                ),
              );
            }));
  }
}

class RoundedSearchFieldController extends GetxController {
  RoundedSearchFieldController({required this.onSearchResult});

  BrnSearchTextController searchController = BrnSearchTextController();
  TextEditingController textController = TextEditingController();
  FocusNode focusNode = FocusNode();
  RxDouble height = 70.0.obs;
  RxInt count = 0.obs;
  List<BMFSuggestionInfo> suggestList = [];
  var city = "常州市"; //搜索的地级市名称
  void Function(BMFSuggestionInfo) onSearchResult;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    focusNode.unfocus();
  }

  //根据输入的内容搜索相应的位置
  void searchLocation(info) async {
    BMFSuggestionSearchOption suggestionSearchOption =
        BMFSuggestionSearchOption(keyword: info, cityname: city);
    BMFSuggestionSearch suggestionSearch = BMFSuggestionSearch();
    suggestionSearch.onGetSuggestSearchResult(callback:
        (BMFSuggestionSearchResult result, BMFSearchErrorCode errorCode) {
      if (result.suggestionList!.isNotEmpty) {
        //有搜索结果
        suggestList = result.suggestionList!;
        count.value = suggestList.length;
        for (int i = 0; i < result.suggestionList!.length; i++) {
          print("提醒位置：${result.suggestionList![i].key}");
        }
      } else {
        print("提醒位置：空");
      }
    });
    await suggestionSearch.suggestionSearch(suggestionSearchOption);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    textController.dispose();
    searchController.dispose();
  }

  recoveryState() {
    searchController.isClearShow = false;//隐藏清空按钮
    searchController.isActionShow = false;//隐藏取消按钮
    focusNode.unfocus();//移除焦点
    height.value = 70.0;//设置初始高度
    count.value = 0;//清空列表
    textController.text = '';//清空输入框
    suggestList.clear();//清空列表
  }
}
